<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
class User_Model extends Model 
{
	private $user="";
	function User_Model()
    {
        parent::Model();
    }
	// ADD NEW USER
	function add_user($data)
	{
		$newUser = array();
        $newUser['first_name'] = $data['first_name'];
		$newUser['last_name'] = $data['last_name'];
        $newUser['email'] = $data['email'];
		$newUser['password'] = md5($data['password']);
		$newUser['country_id'] = $data['country'];
		$newUser['state'] = $data['state'];
        $newUser['city'] = $data['city'];
        
        $newUser['created_date'] = time();
        
        $this->db->insert('users', $newUser);  
		return $this->db->insert_id();

	}
	// check user logins
	function check_login($username,$pass,$rememberme='')
	{

		$this->db->like('email', $username);
		$this->db->like('password', md5($pass)); 
		$query = $this->db->get('users');
		return $query;
	}
	// Update: change pass OR forgot pass
	function change_password($user_ID,$newpassword)
	{
		$data['password']=md5($newpassword);
		$this->db->where('user_id',$user_ID);
		$result=$this->db->update('users',$data);
			if ($result) 
			{
				return 1;
			} 
			else 
			{
				return 0;
			}

	}
	//1: register new user --> check if email is used 
	//2: user forgot password --> check if email is exist --> send mail to confirm 
	function check_email($email)
	{
		$this->db->like('email', $email);
		$result=$this->db->get('users');
		if ($result) 
			{
				return 1;
			} 
			else 
			{
				return 0;
			}
	}
	// user click link in mail to request password --> check if the CODE is correct
	function check_confirm($code)
	{
		
	}
	
	// TELL A FRIEND
	function tellafriend_process($data)
	{
		/*
		$data['name'];
		$data['email'];
		$data['myfriend_email'];
		*/
		
		return;
	}
	// SEND EMAIL
    function sendEmail($emails, $subject, $templateName, $data = array(), $mailType = 'html', $from = null){

	    //$configEmail = $this->config->item('email');
	    $configEmail['mailtype'] = $mailType;
	    $this->load->library('email', $configEmail);
	    $this->email->set_newline("\r\n");
	    $this->email->initialize($configEmail);
	    /**
	     * Load email template from database
	     */
	    $result = $this->db->get_where('cvj_template_mail', array('name'=> $templateName, 'enabled'=> 1));
	    $result = $result->result_array();
	    if (empty($result)){
	        return false;
	    }
	    $content = $result[0];
        extract($data);
        ob_start();
	    echo eval ('?>'.$content['content']);
	    $content = ob_get_contents();
	    @ob_end_clean();
	    /**
	     * Send mail
	     */
	    try {
            $this->email->clear();
            $this->email->to($emails);
            //$this->email->from($message['email'],$message['first_name']." ".$message['surname']);
            $this->email->from('test@bjornekaelderen.dk');
            $this->email->subject($subject);
            $this->email->message($content);
            $this->email->send();
	    }catch (Exception $e){
	        //Do nothing
	    }
	}
    function sendEmail2($emails, $subject, $templateName, $data = array(), $mailType = 'html', $from = null){

	    //$configEmail = $this->config->item('email');
	    $configEmail['mailtype'] = $mailType;
	    $this->load->library('email', $configEmail);
	    $this->email->set_newline("\r\n");
	    $this->email->initialize($configEmail);
	    /**
	     * Load email template from database
	     */
	    $result = $this->db->get_where('cvj_template_mail', array('name'=> $templateName, 'enabled'=> 1));
	    $result = $result->result_array();
	    if (empty($result)){
	        return false;
	    }
	    $content = $result[0];
        extract($data);
        ob_start();
	    echo eval ('?>'.$content['content']);
	    $content = ob_get_contents();
	    @ob_end_clean();
	    /**
	     * Send mail
	     */
	    try {
            $this->email->clear();
            $this->email->to($emails);
            //$this->email->from($message['email'],$message['name']);
            $this->email->from('test@bjornekaelderen.dk');
            $this->email->subject($subject);
            $this->email->message($content);
            $this->email->send();
	    }catch (Exception $e){
	        //Do nothing
	    }
	}	
}
